package com.spzx.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserInfoMapper;
import com.spzx.user.api.domain.UserInfo;
import com.spzx.user.service.IUserInfoService;
import org.springframework.util.DigestUtils;

/**
 * 会员Service业务层处理
 *
 * @author atguigu
 * @date 2024-07-03
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 查询会员列表
     *
     * @param userInfo 会员
     * @return 会员
     */
    @Override
    public List<UserInfo> selectUserInfoList(UserInfo userInfo) {
        return userInfoMapper.selectUserInfoList(userInfo);
    }

    @Override
    public Boolean register(UserInfo userInfo) {
        String phone = userInfo.getPhone();
        String username = userInfo.getUsername();
        String password = userInfo.getPassword();

        long count = this.count(Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getUsername, username)
                .or()
                .eq(UserInfo::getPhone, phone));
        if (count > 0) {
            throw new ServiceException("手机号已被注册");
        }
        //密码加密：
        String encodPwd = DigestUtils.md5DigestAsHex(password.getBytes());
        //保存数据到数据库
        userInfo.setPassword(encodPwd);
        userInfo.setAvatar("https://img1.baidu.com/it/u=2688610469,1661954772&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1203");
        userInfo.setPhone(username);
        userInfo.setStatus(1L);

        return baseMapper.insert(userInfo) > 0;
    }

    @Override
    public UserInfo login(String username, String password) {
        // 1 根据账号查询用户
        UserInfo userInfo = this.getOne(Wrappers.lambdaQuery(UserInfo.class)
                .eq(UserInfo::getUsername, username)
                .last("limit 1 "));
        if (userInfo == null) {
            throw new ServiceException("账号不存在");
        }

        // 2  验证密码是否正确
        String encodPwd = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!StringUtils.equals(encodPwd, userInfo.getPassword())) {
            throw new ServiceException("密码错误，请重新输入！");
        }

        // 3 判用户状态是否正常
        if (userInfo.getStatus() == null || userInfo.getStatus() != 1) {
            throw new ServiceException("账号异常！");
        }


        return userInfo;
    }

    @Cacheable(value = "spzx:user:info", key = "#userId")
    @Override
    public Map<String, String> getLoginUserInfo(Long userId) {
        UserInfo userInfo = this.getById(userId);

        Map<String, String> map = new HashMap<>();
        map.put("nickName", userInfo.getNickName());
        map.put("avatar", userInfo.getAvatar());
        return map;
    }

}
